/**
 * ---------------------------
 * Time: 2017/8/6 18:28.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
define(['app', 'zepto'], function (app, $) {
    app.controller('template-register', function ($http, $scope, Device, logger, $routeParams, templateService, MaintainPlan, ObjectUtils, MaintainTemplate) {

        var init = function () {
            var templateId = parseInt($routeParams.templateId);
            if (templateId == null || Number.isNaN(templateId)) {
                $scope.template = MaintainTemplate.toMaintainTemplate(
                    ObjectUtils.cloneObject($scope.defaultMaintainTemplate), MaintainTemplate);
            } else {
                templateService.getTemplate(templateId, function (r, e) {
                    $scope.template = r;
                    $scope.$digest();
                });
            }
        };

        init();
        // 注册
        $scope.register = function () {
            // 调用业务层的注册逻辑
            templateService.addTemplate(
                $scope.template,
                // 成功后的操作
                function (message, id) {
                    $.alert(message + '[id = ' + id + ']', function () {
                        app.backEvent();
                    });
                }
            )
        };

        $scope.addOtherMaintainPlan = function () {
            $.prompt('输入欲添加的保养计划的名字', function (name) {
                if (name == '') {
                    $.alert('保养计划名不能为空！');
                    return;
                }
                $scope.template.maintainPlans.push(
                    new MaintainPlan({
                        maintainName: name
                    })
                );
                $scope.$digest();
            })
        };

        // 更多...
    });
});